﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace TravianHelper.oasis
{
    public class OasisManager
    {
        public DateTime getSyncArrivalTime(Oasis oasis)
        {
            DateTime defaultValue = DateTime.Parse("1/1/0001 12:00:00 AM");
            if (oasis.lastArrival.ToString().Contains( "1/1/0001 12:00:00 AM") && oasis.troopArrivalTime.ToString().Contains( "1/1/0001 12:00:00 AM"))
                return DateTime.Now;
            else if (oasis.lastArrival.ToString().Contains( "1/1/0001 12:00:00 AM"))
                return oasis.troopArrivalTime .ToString().Contains("1/1/0001 12:00:00 AM" )? DateTime.Now : oasis.troopArrivalTime;
            else if (oasis.troopArrivalTime.ToString() .Contains("1/1/0001 12:00:00 AM"))
                return oasis.lastArrival;
            else
                return oasis.lastArrival.CompareTo(oasis.troopArrivalTime) > 0 ? oasis.lastArrival : oasis.troopArrivalTime;
        }

        public int calculateRessourcesInOasis(Oasis oasis, DateTime estimatedArrivalTime)
        {
            int returnVal;
            int production;
            switch (oasis.oasisType)
            {
             
                case (Oasis.OASIS_25):
                    production = 70;
                    break;
                case (Oasis.OASIS_50):
                    production = 110;
                    break;
                case (Oasis.OASIS_25_25):
                    production = 100;
                    break;
                default:
            production = 100;
            break;
            }
            double timeLap = (estimatedArrivalTime.Ticks - getSyncArrivalTime( oasis).Ticks) /(10000d * 1000d* 60d *60d ); //A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond.
            returnVal = (int) Math.Abs(production * timeLap);
            return returnVal;
        }

        public void updateNearestVillage(Oasis zone, ArrayList villages)
        {
            if (zone.distance2Nearest != -1 && zone.nearestVillage != null)
                return;

            for (int j = 0; j < villages.Count; j++)
            {
                Village village = (Village)villages[j];
                double tempDistance = Math.Sqrt((zone.x - village.x) * (zone.x - village.x) + (zone.y - village.y) * (zone.y - village.y));
                if (zone.distance2Nearest == -1)
                {
                    // how far to the nearest village
                    zone.distance2Nearest = tempDistance;
                    zone.nearestVillage = village;
                }
                if (zone.distance2Nearest > tempDistance)
                {
                    zone.distance2Nearest = tempDistance;
                    zone.nearestVillage = village;
                }

            }
        }

    }
}
